const {
    mysql,
    pool,
    router,
    Result,
    Myquery
} = require('../../connect');

const UUID = require('uuid')

router.post('/uptext', (req, res) => {
    uptext(req, res)
})
router.post('/upBook', (req, res) => {
    upBook(req, res)
})

// 插入数据
function uptext(req, res) {
    // 取传过来的text参数
    var textAll = req.body;
    console.log(req.params, req.query, req.body, '参数')
    if (!textAll.name || !textAll.age || !textAll.text) {
        res.json(new Result({
            msg: '信息未输入完整',
            code: 500
        }))
        return;
    }
    var sql = "INSERT INTO `node_one` (id,name,age,`text`) VALUES (?,?,?,?)"
    var inserts = [UUID.v4().replace(/-/g, ''), textAll.name, textAll.age, textAll.text]
    // 循环插入
    sql = mysql.format(sql, inserts)
    console.log(sql)
    new Myquery(sql, (err, r) => {
        console.log(err,'err')
        let code = 200,
            data = r,
            msg = '成功';
        if (err) {
            code = 500;
            msg = "失败";
            data = null
        }
        res.json(new Result({
            msg,
            data,
            code
        }))
    })
}
// 插入数据
function upBook(req, res) {
    // 取传过来的text参数
    var textAll = req.body;
    // console.log(req.params, req.query, req.body, '参数')
    if (!textAll.title || !textAll.imgUrl) {
        res.json(new Result({
            msg: '信息未输入完整',
            code: 500
        }))
        return;
    }
    console.log(textAll.title)
    var sql = "INSERT INTO `book` (id,title,imgUrl,`html`) VALUES (?,?,?,?)"
    var inserts = [UUID.v4().replace(/-/g, ''), textAll.title, textAll.imgUrl, textAll.html]
    // 循环插入
    sql = mysql.format(sql, inserts)
    // console.log(sql)
    new Myquery(sql, (err, r) => {
        // console.log(err,'err')
        let code = 200,
            data = r,
            msg = '成功';
        if (err) {
            code = 500;
            msg = "失败";
            data = err
        }
        res.json(new Result({
            msg,
            data,
            code
        }))
    })
}

module.exports = router